/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.achartengine.chartdemo.demo.slice;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.achartengine.chartdemo.demo.ResourceTable;
import org.achartengine.chartdemo.demo.SampleItem;
import org.achartengine.chartdemo.demo.SampleItemProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Color;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.TimeChart;

import org.achartengine.model.CategorySeries;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;


public class GeneratedChartDemo extends AbilitySlice {
    private static final int SERIES_NR = 2;

    private String[] mMenuText;

    private String[] mMenuSummary;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        mMenuText = new String[]{"Line chart", "Scatter chart", "Time chart", "Bar chart"};
        mMenuSummary = new String[]{"Line chart with randomly generated values",
                "Scatter chart with randomly generated values",
                "Time chart with randomly generated values", "Bar chart with randomly generated values"};
    }

    @Override
    protected void onActive() {
        super.onActive();
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        SampleItemProvider sampleItemProvider = new SampleItemProvider(getListValues(), this);
        listContainer.setItemProvider(sampleItemProvider);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
                                                 @Override
                                                 public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                                                     switch (i) {
                                                         case 0:
                                                             Intent intent = ChartFactory.getLineChartIntent(GeneratedChartDemo.this, getDemoDataset(), getDemoRenderer());
                                                             startAbility(intent);
                                                             break;
                                                         case 1:
                                                             intent = ChartFactory.getScatterChartIntent(GeneratedChartDemo.this, getDemoDataset(), getDemoRenderer());
                                                             startAbility(intent);
                                                             break;
                                                         case 2:
                                                             intent = ChartFactory.getTimeChartIntent(GeneratedChartDemo.this, getDateDemoDataset(), getDemoRenderer(), null);
                                                             startAbility(intent);
                                                             break;
                                                         case 3:
                                                             XYMultipleSeriesRenderer renderer = getBarDemoRenderer();
                                                             setChartSettings(renderer);
                                                             intent = ChartFactory.getBarChartIntent(GeneratedChartDemo.this, getBarDemoDataset(), renderer, Type.DEFAULT);
                                                             startAbility(intent);
                                                             break;
                                                     }
                                                 }
                                             }
        );
    }

    private List<SampleItem> getListValues() {
        List<SampleItem> values = new ArrayList<SampleItem>();
        int length = mMenuText.length;
        for (int i = 0; i < length; i++) {
            SampleItem v = new SampleItem();
            v.setName(mMenuText[i]);
            values.add(v);
        }
        return values;
    }


    private XYMultipleSeriesDataset getDemoDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        final int nr = 10;
        SecureRandom r = new SecureRandom();
        for (int i = 0; i < SERIES_NR; i++) {
            XYSeries series = new XYSeries("Demo series " + (i + 1));
            for (int k = 0; k < nr; k++) {
                series.add(k, 20 + r.nextInt() % 100);
            }
            dataset.addSeries(series);
        }
        return dataset;
    }

    private XYMultipleSeriesDataset getDateDemoDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        final int nr = 10;
        long value = new Date().getTime() - 3 * TimeChart.DAY;
        SecureRandom r = new SecureRandom();
        for (int i = 0; i < SERIES_NR; i++) {
            TimeSeries series = new TimeSeries("Demo series " + (i + 1));
            for (int k = 0; k < nr; k++) {
                series.add(new Date(value + k * TimeChart.DAY / 4), 20 + r.nextInt() % 100);
            }
            dataset.addSeries(series);
        }
        return dataset;
    }

    private XYMultipleSeriesDataset getBarDemoDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        final int nr = 10;
        SecureRandom r = new SecureRandom();
        for (int i = 0; i < SERIES_NR; i++) {
            CategorySeries series = new CategorySeries("Demo series " + (i + 1));
            for (int k = 0; k < nr; k++) {
                series.add(100 + r.nextInt() % 100);
            }
            dataset.addSeries(series.toXYSeries());
        }
        return dataset;
    }

    private XYMultipleSeriesRenderer getDemoRenderer() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setPointSize(5f);
        renderer.setMargins(new int[]{20, 30, 15, 0});
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(Color.BLUE.getValue());
        r.setPointStyle(PointStyle.SQUARE);
        r.setFillBelowLine(true);
        r.setFillBelowLineColor(Color.WHITE.getValue());
        r.setFillPoints(true);
        renderer.addSeriesRenderer(r);
        r = new XYSeriesRenderer();
        r.setPointStyle(PointStyle.CIRCLE);
        r.setColor(Color.GREEN.getValue());
        r.setFillPoints(true);
        renderer.addSeriesRenderer(r);
        renderer.setAxesColor(Color.DKGRAY.getValue());
        renderer.setLabelsColor(Color.LTGRAY.getValue());
        return renderer;
    }

    public XYMultipleSeriesRenderer getBarDemoRenderer() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setMargins(new int[]{20, 30, 15, 0});
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(Color.BLUE.getValue());
        renderer.addSeriesRenderer(r);
        r = new XYSeriesRenderer();
        r.setColor(Color.GREEN.getValue());
        renderer.addSeriesRenderer(r);
        return renderer;
    }

    private void setChartSettings(XYMultipleSeriesRenderer renderer) {
        renderer.setChartTitle("Chart demo");
        renderer.setXTitle("x values");
        renderer.setYTitle("y values");
        renderer.setXAxisMin(0.5);
        renderer.setXAxisMax(10.5);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(210);
    }

//  @Override
//  protected void onListItemClick(ListView l, View v, int position, long id) {
//    super.onListItemClick(l, v, position, id);
//    switch (position) {
//    case 0:
//      Intent intent = ChartFactory.getLineChartIntent(this, getDemoDataset(), getDemoRenderer());
//      startActivity(intent);
//      break;
//    case 1:
//      intent = ChartFactory.getScatterChartIntent(this, getDemoDataset(), getDemoRenderer());
//      startActivity(intent);
//      break;
//    case 2:
//      intent = ChartFactory.getTimeChartIntent(this, getDateDemoDataset(), getDemoRenderer(), null);
//      startActivity(intent);
//      break;
//    case 3:
//      XYMultipleSeriesRenderer renderer = getBarDemoRenderer();
//      setChartSettings(renderer);
//      intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT);
//      startActivity(intent);
//      break;
//    }
//  }
}